<?php defined("MAIN_DIR") or die("No direct access");

$vacancy_id = UV::readInt($route->getPath(4));

$edit_error = false;
if (isset($_POST['action']) && $_POST['action'] == "edit")
{
	$title = filter_var($_POST['title'],FILTER_SANITIZE_MAGIC_QUOTES);
	if (isset($_POST['by_agree']))
		$salary = 0;
	else
		$salary = filter_var($_POST['salary'],FILTER_SANITIZE_MAGIC_QUOTES);
	$education = $_POST['education'];
	$languages = filter_var($_POST['languages'],FILTER_SANITIZE_MAGIC_QUOTES);
	$phone = filter_var($_POST['phone'],FILTER_SANITIZE_MAGIC_QUOTES);
	$fax = trim(filter_var($_POST['fax'],FILTER_SANITIZE_MAGIC_QUOTES));
	$email = trim(filter_var($_POST['email'],FILTER_SANITIZE_MAGIC_QUOTES));
	$experience = trim(filter_var($_POST['experience'],FILTER_SANITIZE_MAGIC_QUOTES));
	$demands = trim(filter_var($_POST['demands'],FILTER_SANITIZE_MAGIC_QUOTES));
	$acting = trim(filter_var($_POST['acting'],FILTER_SANITIZE_MAGIC_QUOTES));
	$edit_query = "update `vacancy` set `category_id`='$_POST[category]',`title`='$title',`bank_id`='$_POST[bank]',`salary`='$salary',`education`='$education',`languages`='$languages',`phone`='$phone',`fax`='$fax',`email`='$email',`experience`='$experience',`demands`='$demands',`acting`='$acting' where `vacancy_id` = '$_POST[vacancy_id]' limit 1";
	if (!mysql_query($edit_query))
		$edit_error = true;
}
if ($vacancy_id)
{
	$vacancy_res = mysql_query("select * from `vacancy` where `vacancy_id` = '$vacancy_id' limit 1");
	
	if (mysql_num_rows($vacancy_res) == 0) {
		to("/admin/vacancies");
	}
	
	$vacancy_row = mysql_fetch_array($vacancy_res);
	
	$city_path = $db->getCell("select `C`.`path` from `city` as `C`,`bank` as `B` where `B`.`bank_id` = '$vacancy_row[bank_id]' and `B`.`city_id` = `C`.`city_id` limit 1");
}

require_once ADMIN.HEADFILE;
?>
<script type="text/javascript">
$(document).ready(function(){
	$("#by_agree").click(function(){
		if ($(this).attr("checked"))
			$("#salary").attr("disabled","disabled");
		else
			$("#salary").removeAttr("disabled");
	})
})
</script>
<div align="left">
<h1>Редактирование вакансии</h1>
<hr /><?php
if (isset($_POST['action']) && $_POST['action'] == "edit" && !$edit_error)
	echo "<p style='color:green'>Успешно отредактировано</p>";
elseif ($edit_error)
	echo "<p style='color:red'>Не удалось отредактировать</p>";
?><form method='POST'>
<input type='hidden' name='action' value='edit' />
<input type='hidden' name='vacancy_id' value='<?php echo $vacancy_row['vacancy_id']; ?>' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="150px">Ссылка на сайте</td>
		<td width="700px">
			<a href='<?php echo "/$city_path/banks/vacancies/".$vacancy_row['vacancy_id'];?>' target="_blank"><?php echo $vacancy_row['title'];?></a>
		</td>
	</tr>
	<tr>
		<td>ID</td>
		<td><input style="width:200px" type='text' name='vacancy_id' value='<?php echo $vacancy_row['vacancy_id'];?>' disabled /></td>
	</tr>
	<tr>
		<td>Заголовок</td>
		<td><input style='width:100%' type='text' name='title' value='<?php echo $vacancy_row['title'];?>' /></td>
	</tr>
	<tr>
		<td>Категория</td>
		<td>
			<select style="width:200px" name='category'><?php
			$category_res = mysql_query("select `category_id`,`title` from `vacancy_categories` order by `title`");
			while ($category_row = mysql_fetch_array($category_res))
			{
				echo "<option value='$category_row[category_id]'";
				if ($category_row['category_id'] == $vacancy_row['category_id'])
					echo " selected='selected'";
				echo ">$category_row[title]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank'><?php
			$bank_res = mysql_query("select `bank_id`,`name` from `bank` where `city_id` = '$_SESSION[city_admin]' order by `name`");
			while ($bank_row = mysql_fetch_array($bank_res))
			{
				echo "<option value='$bank_row[bank_id]'";
				if ($bank_row['bank_id'] == $vacancy_row['bank_id'])
					echo " selected='selected'";
				echo ">$bank_row[name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Зарплата</td>
		<td>
			<input id="salary" style='width:200px' type='text' name='salary' <?php
				if ($vacancy_row['salary'] == 0)
					echo " disabled='disabled'";
				else
					echo "value='$vacancy_row[salary]'";?> />
			<input type="checkbox" name="by_agree" id="by_agree" <?php
			if ($vacancy_row['salary'] == 0)
					echo " checked='checked'";
			?> /> По договоренности
		</td>
	</tr>
	<tr>
		<td>Образование</td>
		<td>
			<select style="width:200px" name='education'><?php
			foreach ($education_array as $key => $value)
			{
				echo "<option value='$key'";
				if ($key == $vacancy_row['education'])
					echo " selected='selected'";
				echo ">$value</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Знание языков</td>
		<td><input style="width:100%" type='text' name='languages' value='<?php echo $vacancy_row['languages']?>' /></td>
	</tr>
	<tr>
		<td>Телефон</td>
		<td><input style='width:100%' type='text' name='phone' value='<?php echo $vacancy_row['phone']?>' /></td>
	</tr>
	<tr>
		<td>Факс</td>
		<td><input style='width:100%' type='text' name='fax' value='<?php echo $vacancy_row['fax']?>' /></td>
	</tr>
	<tr>
		<td>E-Mail</td>
		<td><input style='width:100%' type='text' name='email' value='<?php echo $vacancy_row['email'];?>' /></td>
	</tr>
	<tr valign='top'>
		<td>Опыт работы</td>
		<td><textarea style='width:100%' rows='5' name='experience'><?php echo $vacancy_row['experience'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td>Требования</td>
		<td><textarea style='width:100%' rows='20' name='demands'><?php echo $vacancy_row['demands'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td>Обязанности</td>
		<td><textarea style='width:100%' rows='20' name='acting'><?php echo $vacancy_row['acting'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Сохранить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>